package server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

public class Command {

	public static List<String> openUrl(String urlStr){
		List<String> result = new ArrayList<String>();
		URL url; 
		String inputLine;
		result.add("");
		if (urlStr.equals(""))
			return result;
		result.clear();

		try {
			url = new URL(urlStr);
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
			while ((inputLine = in.readLine()) != null)
				result.add(inputLine);
			in.close();

		} catch (Exception e){}
		return result;
	}

	public static void sendGetRequest(String urlStr){
		try {

			String params = URLEncoder.encode("param", "UTF-8")
					+ "=" + URLEncoder.encode("value1", "UTF-8");
			params += "&" + URLEncoder.encode("param2", "UTF-8")
					+ "=" + URLEncoder.encode("value2", "UTF-8");

			String hostname = "vorgurak.servehttp.com";
			int port = 80;

			InetAddress addr = InetAddress.getByName(hostname);
			Socket socket = new Socket(addr, port);
			String path = "/4/proov.php";

			// Send headers
			BufferedWriter wr =
					new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
			wr.write("POST "+path+" HTTP/1.0\r\n");
			wr.write("Content-Length: "+params.length()+"\r\n");
			wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
			wr.write("\r\n");

			// Send parameters
			wr.write(params);
			wr.flush();

		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
